package com.example.thread;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 异步任务调度门面
 */
@Service
public class TaskExecutorService {

    private final ThreadPoolExecutor executor;

    @Autowired
    public TaskExecutorService(ThreadPoolExecutor executor) {
        this.executor = executor;
    }

    /**
     * 启动一个倒计时任务
     */
    public void executeTimeTask(int seconds) {
        System.out.println("新增计时任务，将运行 " + seconds + " 秒");
        executor.execute(new TimeTask(seconds));
    }

    /**
     * 关闭线程池
     */
    public void shutdownExecutor() {
        executor.shutdown();
    }
}

